package com.bite.book.controller;


import com.bite.book.constant.Constant;
import com.bite.book.model.*;
import com.bite.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    public BookService bookService;

    @RequestMapping("/getList")
    public List<BookInfo> getList(){
        List<BookInfo> booklist = bookService.getList();
        return booklist;
    }

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(@RequestBody BookInfo bookInfo){
        System.out.println(bookInfo.toString());

        if(bookInfo.getBookName()==null || bookInfo.getBookName().length()>=30|| bookInfo.getBookName().length()<=0){
            return "图书名字有误";
        }
        if(bookService.selectByName(bookInfo.getBookName())>0){
            return "此图书已存在";
        }
        if(bookInfo.getAuthor()==null || bookInfo.getAuthor().length()>=15 || bookInfo.getAuthor().length()<=0){
            return "作者名字有误";
        }
        if(bookInfo.getCount()==null || bookInfo.getCount()<0){
            return "图书余量有误";
        }
        if(bookInfo.getPrice()==null || bookInfo.getPrice()<=0){
            return "图书价格有误";
        }
        if(bookInfo.getPublish()==null || bookInfo.getPublish().length()<=0){
            return "出版社有误";
        }
        bookService.addbook(bookInfo);
        return "";
    }

    @RequestMapping("/getListByPage")
    public Result<UserInfo> getListByPage(PageRequest pageRequest, HttpSession session){
        ResponseResult<BookInfo> result = bookService.getListByPage(pageRequest);
        return Result.success(result);
    }

    @RequestMapping("/selectById")
    public BookInfo selectById(Integer bookId){
        if(bookId==null){
            return null;
        }
        log.info("修改图书id:"+bookId);
        try {
            return bookService.selectById(bookId);

        }catch (Exception e){
            return null;
        }
    }

    @RequestMapping(value = "/updateById" ,produces="appliction/json")
    public String updateById(BookInfo bookInfo){
        log.info("修改图书:"+bookInfo);
        try {
            bookService.updateById(bookInfo);
            return "";
        }catch (Exception e){
            log.error("参数有误:"+e);
            return "参数有误";
        }
    }

    @RequestMapping(value = "/deleteById",produces = "appliction/json")
    public String deleteById(Integer id){
        log.info("删除图书id:"+id);
        try {
            bookService.deleteById(id);
            return "";
        }catch (Exception e){
            log.error("参数有误:"+e);
            return "参数有误";
        }
    }

    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书id="+ids);
        try {
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败 e:{}"+e);
            return false;
        }
    }

}






